﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sys_dept_tree.aspx.cs" Inherits="Ccit.SysPlat.Organ.sys_dept_tree" %>
<%@ Register assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="Microsoft.Web.UI.WebControls" tagprefix="iewc" %>

<html>
<head runat="server">
    <title></title>
    <script language="javascript" src="../script/jquery.js" type="text/javascript"></script>
    <script language="javascript" src="../script/StringClass_Ext.js" type="text/javascript"></script>
    <script language="javascript" src="../script/jbase64.js" type="text/javascript"></script>
    <style>
		.skin { BORDER-RIGHT: buttonhighlight 2px outset; BORDER-TOP: buttonhighlight 2px outset; FONT-SIZE: 9pt; VISIBILITY: hidden; BORDER-LEFT: buttonhighlight 2px outset; WIDTH: 100px; CURSOR: default; BORDER-BOTTOM: buttonhighlight 2px outset; FONT-FAMILY: Arial, Helvetica, sans-serif; POSITION: absolute; BACKGROUND-COLOR: menu; TEXT-ALIGN: left }
		.menuitems { PADDING-RIGHT: 4px; PADDING-LEFT: 4px; PADDING-TOP: 4px; PADDING-BOTTOM: 4px;}
	</style>
</head>
<body onclick="hideMenu()">
    <form id="form1" runat="server"> 
    <input type="hidden" id="_hidOrganId" runat="server" />
    <input type="hidden" id="_hidOriginUpDeptId" runat="server" />
    <input type="hidden" id="_hidUpDeptId" runat="server" />
    <input type="hidden" id="_hidAddNewSign" runat="server" /> <!-- 新增记录标志 -->
    <input type="hidden" id="_hidDelDeptId" runat="server" />  
    <input type="hidden" id="_hidCurDeptExpr" runat="server" />
    <input type="hidden" id="_hidExpandSign" runat="server" />
    <input type="hidden" id="_hidLevelStruct" runat="server" />   
    <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server">
    </asp:ScriptManager>    
    <div id="root" class="skin" style="LEFT:0px; VISIBILITY:hidden; WIDTH:100px; TOP:0px; HEIGHT:35px">
        <div onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" style="BORDER-BOTTOM: window 2px inset;" class="menuitems" func="AddSubDept">            
                <img src="../images/toolbar/addnew.gif" border="0" />&nbsp; 新增子部门            
        </div>
        <div onclick="clickItem()" onMouseover="highlighItem()" onMouseout="lowlightItem()" class="menuitems" func="DeleteDept">
            <img src="../images/toolbar/delete.gif" border="0" />&nbsp; 删除部门
        </div>
    </div>
    <iewc:TreeView ID="trvDept" runat="server">
    </iewc:TreeView>
    <br />
    <input type="button" id="btnTrigger" onclick="doTrigger()" style="display:none" value="触发事件" />
    <input type="button" id="btnDelTrigger" onclick="doDelTrigger()" style="display:none" value="删除触发事件" />
    </form>
    
    <script type="text/javascript">
    <!--
        var menuskin = "skin";
        var node = null;
        var menuname = "root";

        function hideMenu() {
            var obj = document.getElementById(menuname);
            if (null != obj && typeof (obj) != "undefined")
                document.getElementById(menuname).style.visibility = "hidden";
        }

        function highlighItem() {
            if (event.srcElement.className == "menuitems") {
                event.srcElement.style.backgroundColor = "highlight";
                event.srcElement.style.color = "white";
            }
        }
        function lowlightItem() {
            if (event.srcElement.className == "menuitems") {
                event.srcElement.style.backgroundColor = "";
                event.srcElement.style.color = "black";
                window.status = "";
            }
        }
        function clickItem() {
            if (node != null) {
                var vNodeId = node.getAttribute("ID");
                //alert(vNodeId);				
                var nodeType = node.getAttribute("type").toUpperCase();

                var vNodeIdArray = vNodeId.split("_");
                var vDeptId = vNodeIdArray[0];
                var vOrganId = $get("_hidOrganId").value;

                if (event.srcElement.className == "menuitems" && event.srcElement.disabled != true) {
                    var sFuncName = event.srcElement.getAttribute("func");
                    switch (sFuncName) {
                        case "AddSubDept": //新增子部门
                            var vPageParam = "organId=" + vOrganId + "&updeptId=" + vDeptId;
                            var encodeParm = base64encode(vPageParam);
                            parent.frames[1].location.href = "./sys_dept_edit.aspx?" + encodeParm;
                            break;
                        case "DeleteDept": //删除部门						    
                            if (!confirm("删除当前部门及所有下级子部门,是否继续?"))
                                return;

                            var nodeIndex = node.getNodeIndex();
                            PageMethods.DeleteDept(vDeptId, nodeIndex, ClientCallBack);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        function trvDept.onexpand() {
            /* Step 1：找到当前点击的TreeNode */
            var nodeIndex = event.treeNodeIndex;
            //alert(nodeIndex);		
            if (typeof (nodeIndex) == "undefined") {
                node = null;
                return false;
            }

            trvDept.selectedNodeIndex = nodeIndex;
            node = trvDept.getTreeNode(nodeIndex);

            /* Step 2: 获取部门ID，获取部门下的子部门 */
            var vNodeId = node.getAttribute("ID");
            var vNodeIdArray = vNodeId.split("_")
            var vDeptId = vNodeIdArray[0];
            var vLoadSign = vNodeIdArray[1];
            var vOrganId = $get("_hidOrganId").value;

            if (vDeptId != "0" && vLoadSign == "0") {
                var oSubNodes = node.getChildren();
                var oSubNode;
                for (var i = oSubNodes.length - 1; i >= 0; i--) {
                    oSubNode = oSubNodes[i];
                    oSubNode.remove();
                }

                var expandSign = $get("_hidExpandSign").value; //是否逐级展开 1：是
                var levelStruct = $get("_hidLevelStruct").value;
                PageMethods.GetSubDeptsByDeptId(vDeptId, vOrganId, nodeIndex, expandSign, levelStruct, ClientCallBack);

            }
            hideMenu();
        }

        function trvDept.oncollapse() {
            hideMenu();
        }

        function trvDept.oncontextmenu() {
            hideMenu();
            var nodeIndex = event.treeNodeIndex;
            if (typeof (nodeIndex) == "undefined") {
                node = null;
                return false;
            }
            trvDept.selectedNodeIndex = nodeIndex;
            node = trvDept.getTreeNode(nodeIndex);
            var vNodeId = node.getAttribute("ID");
            var vNodeIdArray = vNodeId.split("_");
            var vDeptId = vNodeIdArray[0];

            if (vDeptId == "0") return;  //根节点不显示右键菜单

            var obj = document.getElementById("root");
            if (null == obj || typeof (obj) == "undefined") {
                node = null;
                return false;
            }

            var rightedge = document.body.clientWidth - event.clientX; 	//右边距
            var bottomedge = document.body.clientHeight - event.clientY; //底边距

            if (rightedge < obj.offsetWidth)
                obj.style.left = document.body.scrollLeft + 5 + event.clientX - obj.offsetWidth;
            else
                obj.style.left = document.body.scrollLeft + 5 + event.clientX;

            if (bottomedge < obj.offsetHeight)
                obj.style.top = document.body.scrollTop + 5 + event.clientY - obj.offsetHeight;
            else
                obj.style.top = document.body.scrollTop + 5 + event.clientY;

            obj.style.visibility = "visible";
            return false;
        }

        function ClientCallBack(vResult) {
            eval("var resultObj=" + vResult);
            var vOperType = resultObj.operType;
            var nodeIndex;
            switch (vOperType) {
                case "DYNLOADNODES":  //动态加载树的子节点                    
                    nodeIndex = resultObj.nodeIndex;
                    var subDeptArray = resultObj.depts;
                    var expandSign = resultObj.expandSign;
                    var levelStruct = resultObj.levelStruct;
                    var parentNodeId = resultObj.curNodeId;

                    createSubTreeNodes(nodeIndex, subDeptArray);

                    if (expandSign == "1") {//是逐级展开
                        var parentIdArr = levelStruct.split(",");
                        var iLen = parentIdArr.length;
                        var bGet = false;
                        for (var i = 0; i < iLen; i++) {
                            var colId = parentIdArr[i];
                            if (!bGet) {
                                if (colId == parentNodeId) {
                                    if (i == (iLen - 1)) {//如果到最底层，则逐级展开标志0
                                        $get("_hidExpandSign").value = "0";
                                    }
                                    bGet = true;
                                    continue;
                                }
                            }
                            else {//true，展开下级
                                var ctrlNode = findTreeNode(colId);
                                var nodeAttrId = ctrlNode.getAttribute("ID");
                                var vNodeIdArray = nodeAttrId.split("_")
                                if (vNodeIdArray[1] == "1") {
                                    if (i == (iLen - 1)) {
                                        ctrlNode.setAttribute("EXPANDED", true);
                                    }
                                    else
                                        continue;
                                }
                                else {
                                    ctrlNode.setAttribute("EXPANDED", true);
                                    break;
                                }
                            }
                        }
                    }
                    break;
                case "DEPTDELETE":  //删除部门
                    nodeIndex = resultObj.nodeIndex;
                    var vDelSign = resultObj.resultSign;
                    if (parseInt(vDelSign) > 0) {
                        alert("删除成功!");
                        var node = $get("trvDept").getTreeNode(nodeIndex);
                        node.remove();
                        parent.frames[1].initializeAddNew();
                    }
                    else {
                        alert(resultObj.resultMsg);
                    }
                    break;
                case "GETSTRUCT":  // get node struct tree ids
                    var levelStruct = resultObj.resultIds;
                    $get("_hidLevelStruct").value = levelStruct;
                    var parentIdArr = levelStruct.split(",");
                    var iLen = parentIdArr.length;
                    var deptId = "";
                    var ctrlNode;
                    var nodeAttrId = "";
                    for (var i = 0; i < iLen; i++) {
                        deptId = parentIdArr[i];
                        ctrlNode = findTreeNode(deptId);
                        nodeAttrId = ctrlNode.getAttribute("ID");

                        var vNodeIdArray = nodeAttrId.split("_");
                        ctrlNode.setAttribute("EXPANDED", true);
                        if (vNodeIdArray[1] == "1") { //判断最老祖宗是否展开过 1--：展开过			                
                            continue;
                        }
                        else {
                            //ctrlNode.setAttribute("EXPANDED",true);//没展开连续展开调tvColumn.onexpand()方法展开最老祖宗
                            break;
                        }
                    }
                    break;

            }

            resultObj = null;
        }

        function createSubTreeNodes(nodeIndex, subDeptArray) {
            var node = $get("trvDept").getTreeNode(nodeIndex);
            var vOrganId = $get("_hidOrganId").value;
            var vDeptId;
            var vDeptName;
            var vHasChild;
            var vNavigateUrl;
            var oSubNode;
            var oEmptyNode;

            var itemCount = subDeptArray.length;
            var subDept;
            var vPageParam;
            var encodeParam;

            for (var i = 0; i < itemCount; i++) {
                subDept = subDeptArray[i];
                vDeptId = subDept.deptId;
                vDeptName = subDept.deptName;
                vHasChild = subDept.isHasChild;
                vPageParam = "organId=" + vOrganId + "&deptId=" + vDeptId;
                encodeParm = base64encode(vPageParam);
                vNavigateUrl = "./sys_dept_edit.aspx?" + encodeParm;
                oSubNode = $get("trvDept").createTreeNode();
                oSubNode.setAttribute("ID", vDeptId + "_" + "0");
                oSubNode.setAttribute("Text", vDeptName);
                oSubNode.setAttribute("Type", "root");
                oSubNode.setAttribute("Target", "iFrmEdit");
                oSubNode.setAttribute("ImageUrl", "../images/organ/dept.gif");
                oSubNode.setAttribute("NavigateUrl", vNavigateUrl);

                if (vHasChild == "1") {  //如果该节点下有子节点，则在节点下增加一个空节点
                    oEmptyNode = $get("trvDept").createTreeNode();
                    oSubNode.add(oEmptyNode);
                }
                node.add(oSubNode);
            }

            /* 将节点的加载标志置为1 */
            var vNodeId = node.getAttribute("ID");
            var vNodeIdArray = vNodeId.split("_")
            var vDeptId = vNodeIdArray[0];
            node.setAttribute("ID", vDeptId + "_" + "1");
        }

        function findTreeNode(findDeptId) {
            var ctrlTree = document.getElementById("trvDept");

            var ctrlRoot = ctrlTree.getTreeNode("0");
            var ctrlChildNodes = ctrlRoot.getChildren();

            var ctrlFindNode = recursionFind(ctrlChildNodes, findDeptId);
            return ctrlFindNode;

        }

        function recursionFind(ctrlChildNodes, findDeptId) {
            var iLen = ctrlChildNodes.length;
            var ctrlTreeNode;
            var vNodeId;
            var vNodeIdArray;
            var vDeptId;
            var ctrlSubChildNodes;
            var bFind = false;

            for (var i = 0; i < iLen; i++) {
                ctrlTreeNode = ctrlChildNodes[i];
                vNodeId = ctrlTreeNode.getAttribute("ID");
                vNodeIdArray = vNodeId.split("_")
                vDeptId = vNodeIdArray[0];
                if (vDeptId == findDeptId) {
                    bFind = true;
                    break;
                }
                else {
                    ctrlSubChildNodes = ctrlTreeNode.getChildren();
                    ctrlTreeNode = recursionFind(ctrlSubChildNodes, findDeptId);
                    if (null != ctrlTreeNode) {
                        bFind = true;
                        break;
                    }

                }
            }

            if (bFind)
                return ctrlTreeNode;
            else
                return null;
        }

        function doTrigger() {
            doOperOriParentNode();  // update origin parent node
            doOperDtParentNode();   // update destination parent node
        }

        function doOperOriParentNode() {
            var vOriUpDeptId = $get("_hidOriginUpDeptId").value;
            var vUpDeptId = $get("_hidUpDeptId").value;
            var isAddNew = $get("_hidAddNewSign").value;

            if (isAddNew == "1") {//编辑
                // find current node position index
                eval("var curNodeObj=" + $get("_hidCurDeptExpr").value);
                var curNodeId = curNodeObj.deptId;
                var curNode = findTreeNode(curNodeId);

                if (vUpDeptId == vOriUpDeptId) {  //parent node no change
                    curNode.setAttribute("Text", curNodeObj.deptName);
                    return;
                }

                ctrlOgNode = null;
                vOgNodeIndex = "";
                // get root node object
                var ctrlTree = document.getElementById("trvDept");
                var ctrlRoot = ctrlTree.getTreeNode("0");

                /* 获取原始上级节点对象 */
                if (vOriUpDeptId != "" && vOriUpDeptId != "0") {
                    ctrlOgNode = findTreeNode(vOriUpDeptId);
                    vOgNodeIndex = ctrlOgNode.getNodeIndex();
                }
                else {
                    ctrlOgNode = ctrlRoot;
                    vOgNodeIndex = "0";
                }

                // delete origin node
                curNode.remove();
                curNodeObj = null;
            }

        }
        function doOperDtParentNode() {
            var vOriUpDeptId = $get("_hidOriginUpDeptId").value;
            var vUpDeptId = $get("_hidUpDeptId").value;
            var isAddNew = $get("_hidAddNewSign").value;

            /* 初始化全局变量 */

            ctrlDtNode = null;
            vDtNodeIndex = "";

            // get root node object
            var ctrlTree = document.getElementById("trvDept");
            var ctrlRoot = ctrlTree.getTreeNode("0");


            /* 获取新上级节点对象 */
            if (vUpDeptId != "" && vUpDeptId != "0") {
                ctrlDtNode = findTreeNode(vUpDeptId);
                if (null == ctrlDtNode) {
                    expandDtNode(vUpDeptId);
                    return;
                }
                vDtNodeIndex = ctrlDtNode.getNodeIndex();
            }
            else {
                ctrlDtNode = ctrlRoot;
                vDtNodeIndex = "0";
            }

            var dtNodeId = ctrlDtNode.getAttribute("ID");
            var vNodeIdArray = dtNodeId.split("_")
            var vDeptId = vNodeIdArray[0];
            var vLoadSign = vNodeIdArray[1];

            var curNodeExpr = $get("_hidCurDeptExpr").value;
            eval("var curNodeObj=" + curNodeExpr);
            var subDeptArray = [];
            subDeptArray.push(curNodeObj);

            if (vDeptId == "0") {
                if (isAddNew == "0" || vUpDeptId != vOriUpDeptId) {
                    createSubTreeNodes(vDtNodeIndex, subDeptArray);
                }
                ctrlDtNode.setAttribute("EXPANDED", true);
            }
            else {
                expandParentNode(ctrlDtNode);
                if (vLoadSign == "0") {  // unexpended
                    var childNodes = ctrlDtNode.getChildren();
                    if (null == childNodes || childNodes == "undefined" || childNodes.length == 0) {
                        var oEmptyNode = $get("trvDept").createTreeNode();
                        ctrlDtNode.add(oEmptyNode);
                    }
                    ctrlDtNode.setAttribute("EXPANDED", true);
                }
                else {
                    if (isAddNew == "1") {//编辑
                        if (vUpDeptId == vOriUpDeptId) { // parent node no change
                            return;
                        }
                        else {
                            // add node to destination parent node		                
                            createSubTreeNodes(vDtNodeIndex, subDeptArray);
                        }
                    }
                    else //新增
                    {
                        createSubTreeNodes(vDtNodeIndex, subDeptArray);
                    }

                    ctrlDtNode.setAttribute("EXPANDED", true);
                }
            }

            curNodeObj = null;
        }

        function expandDtNode(dtNodeId) {
            $get("_hidExpandSign").value = "1"; //连续展开标志
            PageMethods.GetNodeLevelStruct(dtNodeId, ClientCallBack);
        }

        function doDelTrigger() {
            var delDeptId = $get("_hidDelDeptId").value;
            ctrlNode = findTreeNode(delDeptId);
            nodeIndex = ctrlNode.getNodeIndex();
            var node = $get("trvDept").getTreeNode(nodeIndex);
            node.remove();
        }

        function expandParentNode(dtNode) {
            var dtParentNode = dtNode.getParent();
            dtParentNode.setAttribute("EXPANDED", true);

            var dtNodeId = dtParentNode.getAttribute("ID");
            var vNodeIdArray = dtNodeId.split("_")
            var vDeptId = vNodeIdArray[0];
            if (vDeptId != "0") {
                expandParentNode(dtParentNode);
            }

        }
    //-->
    </script>    
    </form>
</body>
</html>
